#!/bin/bash
#
# Extract a Mozilla Spidermonkey snapshot via mercurial
#

# The MIT License
#
# Copyright (c) 2009 Nick Galbreath
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

set -e
CONF=$1

echo "Reading configuration ${CONF}..."
source ${CONF}

echo "REPOSITORY  = ${REPOSITORY}"
echo "TAG         = ${TAG}"
echo "VERSION     = ${VERSION}"
echo "RELEASE     = ${RELEASE}"



HERE="`pwd`"
TREEDIR="${HERE}/trees"
TARDIR="${HERE}/tars"
BUILDDIR="${HERE}/tmp"
PULL=true

mkdir -p ${TREEDIR}
mkdir -p ${TARDIR}
mkdir -p ${BUILDDIR}

#  OK, need to make a better CLI, but...
#
#

# Pulling from mercurial is somewhat painful the FIRST time.
#  You have to pull in ALL of the firefox code, you can't pick out
#  particular directoires (like SVN).
# However, subsequent updates are pretty speedy.
#


DEST=${BUILDDIR}/${PKGNAME}-${VERSION}-${RELEASE}
NAME=${PKGNAME}-${VERSION}-${RELEASE}
TARBALL=${NAME}.tar.bz2

if [ ${PULL} ]; then

pushd .
cd ${TREEDIR}
if [ -n "${TAG}" ]; then
    if [ -d "${TAG}" ]; then
	echo "hg update..."
	(cd ${TAG}; hg pull --rev ${TAG} -u)
    else
	echo "hg clone ${REPOSITORY} @ ${TAG}... this will take a while..."
	hg clone --rev ${TAG} http://hg.mozilla.org/${REPOSITORY}/ ${TAG}
    fi
else
    # pull from trunk, no revision number
    if [ -d "${REPOSITORY}" ]; then
	echo "hg update ${REPOSITORY}."
	(cd ${REPOSITORY}; hg pull -u)
    else
	echo "hg clone '${REPOSITORY}'... this will take a while..."
	hg clone  http://hg.mozilla.org/${REPOSITORY}/ ${REPOSITORY}
    fi
fi
popd
fi

if [ -z "${TAG}" ]; then
    TAG=${REPOSITORY}
fi

echo "Extracting javascript"
#remove existing
rm -rf ${DEST}
# copy just js to a new home
cp -r ${TREEDIR}/${TAG}/js/src ${DEST}


# make a limited change log
# full history > 3M
CHANGELOG=${DEST}/ChangeLog.txt
rm -f ${CHANGELOG}
echo "Making ChangeLog..."
(cd ${TREEDIR}/${TAG}/js/src && hg log -v . | head -n 1000 > ${CHANGELOG})

# Find autoconf 2.13
#  Can't use "which" since it might return a non-zero exit code and
#  break the script
#

# Debian/Ubuntu Style
AUTOCONF=/usr/bin/autoconf2.13

if [ ! -x "$AUTOCONF" ]; then
    # RedHat Style
    AUTOCONF=/usr/bin/autoconf-2.13
fi

if [ ! -x "$AUTOCONF" ]; then
    # MacPorts
    AUTOCONF=/opt/local/bin/autoconf213
fi

if [ ! -x "$AUTOCONF" ]; then
    # Mac Homebrew
    AUTOCONF=/usr/local/bin/autoconf213
fi

if [ ! -x "$AUTOCONF" ]; then
    echo "Unable to find autoconf 2.13"
    exit 1
fi

echo "autoconf213 = ${AUTOCONF}"
(cd ${DEST}; ${AUTOCONF})

echo "Making ${TARBALL}..."
rm -f ${TARDIR}/${TARBALL}
tar -C ${BUILDDIR} -cjf ${TARDIR}/${TARBALL} ${PKGNAME}-${VERSION}-${RELEASE}

echo "DONE"
